package com.green.system.controller;

import com.green.system.common.ActiveUser;
import com.green.system.common.DataGridView;
import com.green.system.common.upload.UploadService;
import com.green.system.domain.User;
import com.green.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Green
 * @Date: 2021/11/11/21:36
 * @Description:
 */

@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private UploadService uploadService ;

    @Autowired
    private UserService userService;

    /**
     * 上传文件
     */
    @RequestMapping("uploadFile")
    public Object uploadFile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);

        Map<String,String> map = new HashMap<>();
        map.put("src",path);

        //更新数据库信息
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        User user = activeUser.getUser();
        user.setImgpath(path);
        this.userService.updateUser(user);

        return new DataGridView(map);
    }

    /**
     * 上传商品图片
     */
    @RequestMapping("uploadGoodsFile")
    public Object uploadGoodsFile(MultipartFile mf){
        String path = this.uploadService.uploadImage(mf);

        Map<String,String> map = new HashMap<>();
        map.put("src",path);
        return new DataGridView(map);
    }
}
